package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Clinic_config_dao;
import com.clinic.domain.Clinic_config;
import com.clinic.util.Util_imp;

public class Clinic_config_imp implements Clinic_config_dao {

	SessionFactory session_factory = null;

	public Clinic_config_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_clinic_config(Clinic_config config) {
		System.out.println("Enter - create_clinic_config");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int cg_obj_id = 0;
		try {
			tx = session.beginTransaction();
			cg_obj_id = (Integer) session.save(config);
			tx.commit();
			System.out.println("New Clinic_config Created with ID: " + cg_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Clinic_config Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_clinic_config");
		return cg_obj_id;
	}

	@Override
	public void update_clinic_config(Clinic_config cg) {
		System.out.println("Enter - update_clinic_config");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(cg);
			tx.commit();
			System.out.println("Clinic_config Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Clinic_config Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_clinic_config");

	}

	@Override
	public void delete_clinic_config(int cg_obj_id) {
		System.out.println("Enter - delete_clinic_config");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Clinic_config se = (Clinic_config) session.get(Clinic_config.class, cg_obj_id);
			session.delete(se);
			tx.commit();
			System.out.println("Clinic_config with ID: " + cg_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Clinic_config Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_clinic_config");
	}

	@Override
	public Clinic_config get_clinic_config(int cg_obj_id) {
		System.out.println("Enter - get_clinic_config");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Clinic_config cg = null;
		try {
			tx = session.beginTransaction();
			cg = (Clinic_config) session.createQuery("from Clinic_config where cg_obj_id=:id")
					.setParameter("id", cg_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Time_period: " + cg.getTime_period());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_clinic_config");
		return cg;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Clinic_config> get_all_clinic_config() {
		System.out.println("Enter - get_all_clinic_config");
		Session session = session_factory.openSession();
		List<Clinic_config> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Clinic_config").list();
			tx.commit();
			System.out.println("Number of Clinic_config returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_clinic_config");
		return l;
	}

	@Override
	public Clinic_config persistOrMerge(Clinic_config c) {
		return (Clinic_config) session_factory.getCurrentSession().merge(c);
	}

	@Override
	public int getsize() {
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Clinic_config").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Clinic_config: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}